package controller;

import model.Goods;
import model.Manager;
import model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.IGoodsService;
import service.IOrderService;
import service.IUserService;
import util.DateUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService iUserService;

    @Autowired
    private IGoodsService iGoodsService;
    @Autowired
    private IOrderService iOrderService;


    @RequestMapping("loginPage")
    public ModelAndView loginPage(){
        ModelAndView mv= new ModelAndView();
        mv.setViewName("shop/login");
        return  mv;
    }

    @RequestMapping("login")
    @ResponseBody
    public String login(User user, HttpServletRequest request){
        if(iUserService.login(user)==null){
            return "0";
        }
        HttpSession session = request.getSession();
        session.setAttribute("user",iUserService.login(user));
        session.setMaxInactiveInterval(300);
        return  iUserService.login(user).getId().toString();
    }

    @RequestMapping("registerPage")
    public ModelAndView registerPage(){
        ModelAndView mv= new ModelAndView();
        mv.setViewName("shop/register");
        return  mv;
    }

    @RequestMapping("register")
    @ResponseBody
    public String register(User user){
        String pass= user.getuPass();
        user.setuPass(null);
        if(iUserService.login(user)!=null){
            return "2";
        }
        user.setuPass(pass);
        user.setCreateTime(DateUtil.currentDate());
        int count = iUserService.register(user);
        if(count<1){
            return "0";
        }else {
            return  "1";
        }
    }
    @RequestMapping("centerPage")
    public ModelAndView centerPage(HttpServletRequest request){
        HttpSession session = request.getSession();
        User m = (User) session.getAttribute("user");
        ModelAndView mv = new ModelAndView();
        mv.addObject("obj", iUserService.login(m));
        mv.setViewName("shop/user_center_info");
        return  mv;
    }
    @RequestMapping("centerOrderPage")
    public ModelAndView centerOrderPage(HttpServletRequest request){

        return  iOrderService.listOrderByUId(request);
    }

    @RequestMapping("cartPage")
    public ModelAndView cartPage(HttpServletRequest request){
        HttpSession session = request.getSession();
        User m = (User) session.getAttribute("user");
        ModelAndView mv = new ModelAndView();
        mv.addObject("obj", m);
        mv.setViewName("shop/cart");
        return mv;
    }
    @RequestMapping("rechargePage")
    public ModelAndView rechargePage(HttpServletRequest request){
        HttpSession session = request.getSession();
        User m  = (User)session.getAttribute("user");
        ModelAndView mv = new ModelAndView();
        mv.addObject("user", m);
        mv.setViewName("shop/user_recharge");
        return  mv;
    }
    @RequestMapping("recharge")
    @ResponseBody
    public String recharge(User user, String recharge){
        return iUserService.recharge(user, recharge);
    }
    @RequestMapping("changePassPage")
    public ModelAndView changePassPage(HttpServletRequest request){
        HttpSession session = request.getSession();
        User m = (User)session.getAttribute("user");
        ModelAndView mv = new ModelAndView();
        mv.addObject("user", m);
        mv.setViewName("shop/user_changePass");
        return  mv;
    }
    @RequestMapping("changePass")
    @ResponseBody
    public String changePass(User user){
        return iUserService.changePass(user);
    }
}
